package com.ihmp.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;

import com.ihmp.model.Thotels;
import com.ihmp.model.Troomtypes;
import com.ihmp.utils.HibernateUtils;

public class HotelsDaoImpl implements HotelsDao{


	//private SessionFactory sessionFactory=  HibernateUtils.getSessionFactory();
	private Session session;

	    private Transaction tx ;
	@Override
	public void save(Thotels hotel) {
		try {
            startOperation();
            session.save(hotel);
            tx.commit();
        } catch (HibernateException e) {
            handleException(e);
        } 
        finally {
          session.close();
        }
		
	}

	@Override
	public void update(Thotels hotel) {
		// TODO Auto-generated method stub
		try {
            startOperation();
            session.update(hotel);
            tx.commit();
        } catch (HibernateException e) {
            handleException(e);
        } 
        finally {
          session.close();
        }
		
	}

	@Override
	public void delete(Integer id) {
		// TODO Auto-generated method stub
	
		 try {
	          startOperation();
	      	Thotels hotel = (Thotels)session.load(Thotels.class, id);
			if(null != hotel) session.delete(hotel);
	            tx.commit();
	        } catch (HibernateException e) {
	            handleException(e);
	        } finally {
	        	  session.close();
	        }
	
	}

	@Override
	public List<Thotels> listHotels() {
		// TODO Auto-generated method stub
	
		
	        
	           List<Thotels> objects = null;
		        try {
		        	startOperation();
		         //   Query query = session.createQuery("from " + clazz.getName());
		          //  objects =query.list();
		            objects = session.createQuery("from Thotels").list();
		            tx.commit();
		        } catch (HibernateException e) {
		            handleException(e);
		        } finally {
		        	 // session.close();
		        }
		        return objects;
	       
	}

	@Override
	public Thotels findByID(Integer id) {
		// TODO Auto-generated method stub
		return   (Thotels) session.get(Thotels.class, id);
	}
	
	  protected void session(HibernateException e) {
	        tx.rollback();
	        e.printStackTrace();
	    }
	  
	  protected void handleException(HibernateException e) {
	        tx.rollback();
	        e.printStackTrace();
	    }
	    protected void startOperation() throws HibernateException {

	    	
   	        session = HibernateUtils.getSessionFactory().openSession();

	        tx = session.beginTransaction();
	    }

		@Override
		public <T> void save(T object) {
			// TODO Auto-generated method stub			
		}
	
}
